package com.dchh.android.suverynew.act;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dchh.android.suverynew.R;
import com.dchh.android.suverynew.adapter.TaskOperateAdapter;
import com.dchh.android.suverynew.base.SBaseActivity;
import com.dchh.android.suverynew.presenter.ITaskMainPresenter;
import com.dchh.android.suverynew.presenter.impl.TaskMainPresenter;
import com.dchh.android.suverynew.view.ITaskMainView;
import com.dhcc.android.base.common.bean.Node;
import com.dhcc.android.base.common.bean.Task;
import com.dhcc.android.base.utils.Constants;
import com.dhcc.android.base.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 任务管理
 *
 * @author zhoudan
 * @date 2015年5月18日
 */
public class TaskMainActivity extends SBaseActivity implements ITaskMainView {
    /**
     * 用户中心标签块
     */
    private RelativeLayout userCenter;

    /**
     * 显示任务包的LsitView
     */
    private ListView hasDownListView;
    /**
     * 已下载
     */
    private LinearLayout view2;
    /**
     * 没有数据
     */
    private RelativeLayout nodata2;
    /**
     * 任务包列表集合
     */
    private List<Task> downList = new ArrayList<Task>();
    /**
     * 任务包列表适配器
     */
    private TaskOperateAdapter adapter2;
    /**
     * 层级目录list集合
     */
    private List<Node> nodeList;

    private long mExitTime;

    private ITaskMainPresenter mTaskMainPresenter = new TaskMainPresenter(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task_main);
    }

    @Override
    protected void setTitle(TextView toolbar_title) {
        toolbar_title.setText("任务包列表");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.task_main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_right:
                userCenter.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 跳转到个人中心
                        if (StringUtil.isFastDoubleClick(500)) {
                            return;
                        }
                        Intent intent = new Intent();
                        intent.setClass(TaskMainActivity.this, UserCenterActivity.class);
                        startActivity(intent);

                    }
                });
                break;
        }
        return true;
    }

    /**
     * 初始化控件
     */
    @Override
    public void initView() {
        hasDownListView = (ListView) findViewById(R.id.has_down_view);
        nodata2 = (RelativeLayout) findViewById(R.id.nodata_m2);
        view2 = (LinearLayout) findViewById(R.id.has_down_main_layout);
        // 获取任务包列表
        mTaskMainPresenter.initData();
    }

    @Override
    public void isNoData(boolean noData) {
        if (noData) {
            nodata2.setVisibility(View.VISIBLE);
            hasDownListView.setVisibility(View.GONE);
        } else {
            if (nodata2.getVisibility() == View.VISIBLE) {
                nodata2.setVisibility(View.GONE);
                hasDownListView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onInitDataSuccess(List<Task> data) {
        downList = data;
        // 设置适配器
        if (adapter2 == null) {
            adapter2 = new TaskOperateAdapter(this, downList);
            adapter2.setListener(new StartResearchListener());
            hasDownListView.setAdapter(adapter2);
        }
        mTaskMainPresenter.restore(downList);
    }

    @Override
    public void onRestoreSuccess(String title) {
        Intent intent = new Intent();
        intent.setClass(TaskMainActivity.this, CycleSelectActivity.class);
        intent.putExtra("taskName", title);
        TaskMainActivity.this.startActivity(intent);
    }

    /**
     * 内部类
     * <p/>
     * 开始调查
     *
     * @author Tudou
     */
    public class StartResearchListener {

        public void onStart(int position) {
            if (StringUtil.isFastDoubleClick(500)) {
                return;
            }

            Constants.currentTask = downList.get(position);
            // add by pengling
            // 保存当前任务进度
            Constants.currentCounty = Constants.currentTask.getCounty();

            Intent intent = new Intent();
            intent.setClass(TaskMainActivity.this, CycleSelectActivity.class);
            intent.putExtra("taskName", Constants.currentTask.getTitle());
            TaskMainActivity.this.startActivity(intent);
        }
    }
}
